<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Markdown文本编辑器</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="./elfinder/js/jquery-1.11.1.js"></script>
    <script src="./common/sweetalert2@9.js"></script>
    <script src="./common/utils.js"></script>
    <style type="text/css">
        body{
            margin: 0;
            padding: 0;
            overflow-x: hidden;
        }
        .swal2-container.swal2-backdrop-show, .swal2-container.swal2-noanimation {
            background: rgb(255, 255, 255);
        }
    </style>
    <link rel="stylesheet" href="./editormd/editormd.min.css"/>
</head>
<body>
<div class="row">
    <div id="editor"></div>
</div>
<script type="text/javascript" src="./editormd/editormd.min.js"></script>
<script type="text/javascript">
    (function () {
        var file = queryUrlParam("file");
        var conv = queryUrlParam("conv") || '0';
        if(file){
            swal.fire({
                title: "...",
                showConfirmButton: false,
                imageUrl: "./images/loading@2x.gif",
                showCancelButton: false
            });
            $.ajax({
                type:"GET",
                url:"./elfinder/connector",
                data:{cmd:'get', target:file, conv:conv},
                success:function (res) {
                    window._editor = editormd("editor", {
                        width: "100%",
                        height: window.innerHeight - 40,
                        markdown: res.content,
                        toolbarIcons : function() {
                            return ["save","undo", "redo", "|", "bold","del","italic","quote","ucwords","uppercase","lowercase","h1"
                                ,"h2","h3", "list-ul","list-ol","link","image","code","table","code-block","html-entities"
                                ,"hr","clear","goto-line","search","|", "watch", "preview", "help"]
                        },
                        watch: false,
                        previewCodeHighlight : false,
                        flowChart : true,
                        path:"./editormd/lib/",
                        onload : function() {
                            this.watch();
                            this.unwatch();
                        },
                        onsave: function(){
                            _editor.watch();
                            _editor.unwatch();
                            var file = queryUrlParam("file");
                            if(file){
                                $.ajax({
                                    type:"PUT",
                                    url:"./elfinder/connector",
                                    data:{cmd:'put',target:file,encoding:'UTF-8',content:_editor.getMarkdown()},
                                    success:function(res) {
                                        Swal.fire('保存',"保存成功","success");
                                    },
                                    error:function (err) {
                                        console.error(err);
                                        Swal.fire('保存',"保存失败","error");
                                    }
                                });
                            }
                        }
                    });
                    setTimeout(function(){
                        $(".swal2-center").css("display","none");
                        $("body").removeClass("swal2-shown","swal2-height-auto");
                    }, 1000);
                },
                error:function (err) {
                    $(".swal2-center").css("display","none");
                    $("body").removeClass("swal2-shown","swal2-height-auto");
                    console.error(err);
                }
            });
        }
    })();
</script>
</body>
</html>